#include "homeform.h"
#include "ui_homeform.h"
#include "RightSetting/objectiveform.h"
#include "RightSetting/areaform.h"
#include "RightSetting/zscanform.h"
#include "LeftSetting/controlform.h"

HomeForm::HomeForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomeForm)
{
    ui->setupUi(this);
    
    // 连接LightControlForm的sig_updateLedValue信号到LightSettingsForm的updateLedValue槽函数
    // 这样当调用updateSpinBox时，LED值也会同步更新
    connect(ui->widget_lightControl, &LightControlForm::sig_updateLedValue, 
            ui->widget_lightSettingForm, &LightSettingsForm::updateLedValue);
    
    // 连接LightControlForm的增益、灰度系数、图像增益信号到LightSettingsForm的对应槽函数
    connect(ui->widget_lightControl, &LightControlForm::sig_updateGain, 
            ui->widget_lightSettingForm, &LightSettingsForm::updateGain);
    connect(ui->widget_lightControl, &LightControlForm::sig_updateGamma, 
            ui->widget_lightSettingForm, &LightSettingsForm::updateGamma);
    connect(ui->widget_lightControl, &LightControlForm::sig_updateGainImage, 
            ui->widget_lightSettingForm, &LightSettingsForm::updateGainImage);
    
    // 连接ObjectiveForm的物镜倍数变化信号到ZScanForm的设置倍数槽函数
    connect(ui->widget_objectiveForm, &ObjectiveForm::objectiveMagnificationChanged, 
            ui->widget_areaForm, &AreaForm::setObjectiveMagnification);

    // 连接ObjectiveForm的物镜倍数变化信号到ZScanForm的设置倍数槽函数
    connect(ui->widget_objectiveForm, &ObjectiveForm::objectiveMagnificationChanged,
            ui->widget_zScanForm, &ZScanForm::updateStepFromMagnification);
    
    // 建立ObjectiveForm和ObjectiveChooseForm之间的关联
    ui->widget_objectiveForm->setAssociatedObjectiveChooseForm(ui->widget_objectiveChooseForm);

    // 连接widget_controlForm 绝对位置变化值,与ZScanForm的T范围和B范围绑定
    connect(ui->widget_controlForm, &ControlForm::sig_labelChanged,
            ui->widget_zScanForm, &ZScanForm::updatelabelChanged);

}

HomeForm::~HomeForm()
{
    delete ui;
}

void HomeForm::SetCalibrationCorrection(bool _state)
{
    ui->widget_objectiveChooseForm->SetCalibrationCorrection(_state);
    if(_state)
        ui->stackedWidget_right->setCurrentIndex(1);
    else {
        ui->stackedWidget_right->setCurrentIndex(0);
    }
}

void HomeForm::on_pushButton_confocal_clicked()
{
    int type = 0;
    ui->widget_measurementForm->UpdateShowType(type);
    ui->widget_processingForm->UpdateShowType(type);
    ui->widget_autoFocusForm->UpdateShowType(type);
    ui->widget_thresholdForm->UpdateShowType(type);
    ui->widget_zScanForm->UpdateShowType(type);
    ui->widget_lightSettingForm->UpdateShowType(type);
    ui->widget_objectiveForm->UpdateShowType(type);
    ui->widget_areaForm->UpdateShowType(type);
    ui->widget_analysisForm->UpdateShowType(type);
}

void HomeForm::on_pushButton_fringe_clicked()
{
    int type = 1;
    ui->widget_measurementForm->UpdateShowType(type);
    ui->widget_processingForm->UpdateShowType(type);
    ui->widget_autoFocusForm->UpdateShowType(type);
    ui->widget_thresholdForm->UpdateShowType(type);
    ui->widget_zScanForm->UpdateShowType(type);
    ui->widget_lightSettingForm->UpdateShowType(type);
    ui->widget_objectiveForm->UpdateShowType(type);
    ui->widget_areaForm->UpdateShowType(type);
    ui->widget_analysisForm->UpdateShowType(type);
}

void HomeForm::on_pushButton_multifocal_clicked()
{
    int type = 2;
    ui->widget_measurementForm->UpdateShowType(type);
    ui->widget_processingForm->UpdateShowType(type);
    ui->widget_autoFocusForm->UpdateShowType(type);
    ui->widget_thresholdForm->UpdateShowType(type);
    ui->widget_zScanForm->UpdateShowType(type);
    ui->widget_lightSettingForm->UpdateShowType(type);
    ui->widget_objectiveForm->UpdateShowType(type);
    ui->widget_areaForm->UpdateShowType(type);
    ui->widget_analysisForm->UpdateShowType(type);
}
